﻿using Excel4DB.Util;
using Excel4DB.Util.Interfaces;
using Excel4DBModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Excel4DB.WinApp
{
    public partial class DBForm : Form
    {
        private readonly IDataBase _dataBase;
        public List<SysDataBase> DBList { get; set; }
        public DBForm(IDataBase dataBase)
        {
            _dataBase = dataBase;
            InitializeComponent();
        }

        private void DBForm_Load(object sender, EventArgs e)
        {
            DataTable dbTable = _dataBase.GetDataBase();
            DBList = dbTable.DataTable2List<SysDataBase>();
            this.dbComboBox.Items.AddRange(DBList.Select(c => c.Name).ToArray());
        }

        private void dbComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.tableComboBox.Items.Clear();
            string dbName = this.dbComboBox.SelectedItem.ToString();
            if(!string.IsNullOrWhiteSpace(dbName))
            {
                ConfigHelper.Instance.DBName = dbName;
                DataTable tableDT= _dataBase.GetTables();
                foreach (DataRow row in tableDT.Rows)
                {
                    this.tableComboBox.Items.Add(row["name"].ToString());
                }
            }
        }

        private void tableComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.fieldListBox.Items.Clear();
            string tableName = this.tableComboBox.SelectedItem.ToString();
            if(!string.IsNullOrWhiteSpace(tableName))
            {
                DataTable fieldTable = _dataBase.GetFields(tableName);
                foreach (DataRow row in fieldTable.Rows)
                {
                    string column = row["column"].ToString();
                    if(column.Length>15)
                    {
                        column = column.Substring(0, 15);
                    }
                    else
                    {
                        column = column.PadRight(15);
                    }
                    this.fieldListBox.Items.Add($"{column}\t\t{row["type"]}");
                }
            }
        }

        private void btnDevExcel_Click(object sender, EventArgs e)
        {
            if (fieldListBox.Items.Count == 0) return;
            string tableName = this.tableComboBox.SelectedItem.ToString();
            if (string.IsNullOrWhiteSpace(tableName)) return;
            var dt = _dataBase.GetDataFromTable(tableName);
            string dir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            NPOIHelper.DataTableToExcel(dt, tableName, Path.Combine(dir,$"{tableName}.xls"));
            MessageBox.Show("导出到桌面，Success");
        }

        private void btnCreModel_Click(object sender, EventArgs e)
        {
            string tableName = this.tableComboBox.SelectedItem.ToString();
            StringBuilder sb = new StringBuilder();
            sb.Append($"public class {tableName}");
            sb.Append("\r\n");
            sb.Append("{");
            sb.Append("\r\n");

            string proTemplate = string.Empty;
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                modelgroupBox.Enabled = true;
                modelgroupBox.Visible = true;
                DataTable fieldTable = _dataBase.GetFields(tableName);
                foreach (DataRow row in fieldTable.Rows)
                {
                    proTemplate = @"public {0} {1} {{get;set;}}";
                    sb.Append("   ");
                    if (row["type"].ToString().Contains("time")|| row["type"].ToString().Contains("date"))
                    {
                        sb.Append(string.Format(proTemplate, "DateTime", row["column"].ToString()));
                    }
                    else if (row["type"].ToString().Contains("int"))
                    {
                        sb.Append(string.Format(proTemplate, "int", row["column"].ToString()));
                    }
                    else
                    {
                        sb.Append(string.Format(proTemplate, "string", row["column"].ToString()));
                    }
                    sb.Append("\r\n");
                }
                sb.Append("}");

                this.modelTxtBox.Text = sb.ToString();
            }
        }
    }
}
